

<!DOCTYPE html>
<html class="writer-html5" lang="zh" >
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>条目导出器 &mdash; Scrapy 2.3.0 文档</title>
  

  
  <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster.custom.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster.bundle.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-shadow.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-punk.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-noir.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-light.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/tooltipster-sideTip-borderless.min.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/micromodal.css" type="text/css" />
  <link rel="stylesheet" href="../_static/css/sphinx_rtd_theme.css" type="text/css" />

  
  
  
  

  
  <!--[if lt IE 9]>
    <script src="../_static/js/html5shiv.min.js"></script>
  <![endif]-->
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
        <script src="../_static/jquery.js"></script>
        <script src="../_static/underscore.js"></script>
        <script src="../_static/doctools.js"></script>
        <script src="../_static/language_data.js"></script>
        <script src="../_static/js/hoverxref.js"></script>
        <script src="../_static/js/tooltipster.bundle.min.js"></script>
        <script src="../_static/js/micromodal.min.js"></script>
    
    <script type="text/javascript" src="../_static/js/theme.js"></script>

    
    <link rel="index" title="索引" href="../genindex.html" />
    <link rel="search" title="搜索" href="../search.html" />
    <link rel="next" title="发行说明" href="../news.html" />
    <link rel="prev" title="信号" href="signals.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../index.html" class="icon icon-home" alt="Documentation Home"> Scrapy
          

          
          </a>

          
            
            
              <div class="version">
                2.3
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        
        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">第一步</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../intro/overview.html">Scrapy一目了然</a></li>
<li class="toctree-l1"><a class="reference internal" href="../intro/install.html">安装指南</a></li>
<li class="toctree-l1"><a class="reference internal" href="../intro/tutorial.html">Scrapy 教程</a></li>
<li class="toctree-l1"><a class="reference internal" href="../intro/examples.html">实例</a></li>
</ul>
<p class="caption"><span class="caption-text">基本概念</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="commands.html">命令行工具</a></li>
<li class="toctree-l1"><a class="reference internal" href="spiders.html">蜘蛛</a></li>
<li class="toctree-l1"><a class="reference internal" href="selectors.html">选择器</a></li>
<li class="toctree-l1"><a class="reference internal" href="items.html">项目</a></li>
<li class="toctree-l1"><a class="reference internal" href="loaders.html">项目加载器</a></li>
<li class="toctree-l1"><a class="reference internal" href="shell.html">Scrapy shell</a></li>
<li class="toctree-l1"><a class="reference internal" href="item-pipeline.html">项目管道</a></li>
<li class="toctree-l1"><a class="reference internal" href="feed-exports.html">Feed 导出</a></li>
<li class="toctree-l1"><a class="reference internal" href="request-response.html">请求和响应</a></li>
<li class="toctree-l1"><a class="reference internal" href="link-extractors.html">链接提取器</a></li>
<li class="toctree-l1"><a class="reference internal" href="settings.html">设置</a></li>
<li class="toctree-l1"><a class="reference internal" href="exceptions.html">例外情况</a></li>
</ul>
<p class="caption"><span class="caption-text">内置服务</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="logging.html">登录</a></li>
<li class="toctree-l1"><a class="reference internal" href="stats.html">统计数据集合</a></li>
<li class="toctree-l1"><a class="reference internal" href="email.html">发送电子邮件</a></li>
<li class="toctree-l1"><a class="reference internal" href="telnetconsole.html">远程登录控制台</a></li>
<li class="toctree-l1"><a class="reference internal" href="webservice.html">Web服务</a></li>
</ul>
<p class="caption"><span class="caption-text">解决具体问题</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../faq.html">常见问题</a></li>
<li class="toctree-l1"><a class="reference internal" href="debug.html">调试spiders</a></li>
<li class="toctree-l1"><a class="reference internal" href="contracts.html">蜘蛛合约</a></li>
<li class="toctree-l1"><a class="reference internal" href="practices.html">常用做法</a></li>
<li class="toctree-l1"><a class="reference internal" href="broad-crawls.html">宽爬行</a></li>
<li class="toctree-l1"><a class="reference internal" href="developer-tools.html">使用浏览器的开发人员工具进行抓取</a></li>
<li class="toctree-l1"><a class="reference internal" href="dynamic-content.html">选择动态加载的内容</a></li>
<li class="toctree-l1"><a class="reference internal" href="leaks.html">调试内存泄漏</a></li>
<li class="toctree-l1"><a class="reference internal" href="media-pipeline.html">下载和处理文件和图像</a></li>
<li class="toctree-l1"><a class="reference internal" href="deploy.html">部署蜘蛛</a></li>
<li class="toctree-l1"><a class="reference internal" href="autothrottle.html">AutoThrottle 扩展</a></li>
<li class="toctree-l1"><a class="reference internal" href="benchmarking.html">标杆管理</a></li>
<li class="toctree-l1"><a class="reference internal" href="jobs.html">作业：暂停和恢复爬行</a></li>
<li class="toctree-l1"><a class="reference internal" href="coroutines.html">协同程序</a></li>
<li class="toctree-l1"><a class="reference internal" href="asyncio.html">asyncio</a></li>
</ul>
<p class="caption"><span class="caption-text">扩展Scrapy</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="architecture.html">体系结构概述</a></li>
<li class="toctree-l1"><a class="reference internal" href="downloader-middleware.html">下载器中间件</a></li>
<li class="toctree-l1"><a class="reference internal" href="spider-middleware.html">蜘蛛中间件</a></li>
<li class="toctree-l1"><a class="reference internal" href="extensions.html">扩展</a></li>
<li class="toctree-l1"><a class="reference internal" href="api.html">核心API</a></li>
<li class="toctree-l1"><a class="reference internal" href="signals.html">信号</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">条目导出器</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#using-item-exporters">使用项目导出器</a></li>
<li class="toctree-l2"><a class="reference internal" href="#serialization-of-item-fields">项字段的序列化</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#declaring-a-serializer-in-the-field">1。在字段中声明序列化程序</a></li>
<li class="toctree-l3"><a class="reference internal" href="#overriding-the-serialize-field-method">2。重写serialize_field（）方法</a></li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="#built-in-item-exporters-reference">内置项导出器引用</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#baseitemexporter">BaseItemExporter</a></li>
<li class="toctree-l3"><a class="reference internal" href="#pythonitemexporter">PythonItemExporter</a></li>
<li class="toctree-l3"><a class="reference internal" href="#xmlitemexporter">XmlItemExporter</a></li>
<li class="toctree-l3"><a class="reference internal" href="#csvitemexporter">CsvItemExporter</a></li>
<li class="toctree-l3"><a class="reference internal" href="#pickleitemexporter">PickleItemExporter</a></li>
<li class="toctree-l3"><a class="reference internal" href="#pprintitemexporter">PprintItemExporter</a></li>
<li class="toctree-l3"><a class="reference internal" href="#jsonitemexporter">JsonItemExporter</a></li>
<li class="toctree-l3"><a class="reference internal" href="#jsonlinesitemexporter">JsonLinesItemExporter</a></li>
<li class="toctree-l3"><a class="reference internal" href="#marshalitemexporter">MarshalItemExporter</a></li>
</ul>
</li>
</ul>
</li>
</ul>
<p class="caption"><span class="caption-text">其余所有</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../news.html">发行说明</a></li>
<li class="toctree-l1"><a class="reference internal" href="../contributing.html">为 Scrapy 贡献</a></li>
<li class="toctree-l1"><a class="reference internal" href="../versioning.html">版本控制和API稳定性</a></li>
</ul>

            
          
        </div>
        
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../index.html">Scrapy</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../index.html" class="icon icon-home"></a> &raquo;</li>
        
      <li>条目导出器</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="module-scrapy.exporters">
<span id="item-exporters"></span><span id="topics-exporters"></span><h1>条目导出器<a class="headerlink" href="#module-scrapy.exporters" title="永久链接至标题">¶</a></h1>
<p>一旦抓取了项目，您通常希望保留或导出这些项目，以便在其他应用程序中使用数据。这毕竟是抓取过程的全部目的。</p>
<p>为此，Scrapy为不同的输出格式（如XML、CSV或JSON）提供了一组项目导出器。</p><script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<ins class="adsbygoogle"
     style="display:block; text-align:center;"
     data-ad-layout="in-article"
     data-ad-format="fluid"
     data-ad-client="ca-pub-1466963416408457"
     data-ad-slot="8850786025"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
<div class="section" id="using-item-exporters">
<h2>使用项目导出器<a class="headerlink" href="#using-item-exporters" title="永久链接至标题">¶</a></h2>
<p>如果您很着急，只想使用项目导出器输出 爬取  的数据，请参见 <a class="reference internal" href="feed-exports.html#topics-feed-exports"><span class="std std-ref">Feed 导出</span></a> . 否则，如果您想知道项目导出器是如何工作的，或者需要更多的自定义功能（不包括在默认导出中），请继续阅读下面的内容。</p>
<p>为了使用项导出器，必须用它所需的参数实例化它。每个项目导出器需要不同的参数，因此请检查每个导出器文档以确保 <a class="reference internal" href="#topics-exporters-reference"><span class="std std-ref">内置项导出器引用</span></a> . 在实例化导出器之后，必须：</p>
<p>1。调用方法 <a class="reference internal" href="#scrapy.exporters.BaseItemExporter.start_exporting" title="scrapy.exporters.BaseItemExporter.start_exporting"><code class="xref py py-meth docutils literal notranslate"><span class="pre">start_exporting()</span></code></a> 以便发出出口过程开始的信号</p>
<p>2。打电话给 <a class="reference internal" href="#scrapy.exporters.BaseItemExporter.export_item" title="scrapy.exporters.BaseItemExporter.export_item"><code class="xref py py-meth docutils literal notranslate"><span class="pre">export_item()</span></code></a> 要导出的每个项的方法</p>
<p>三。最后打电话给 <a class="reference internal" href="#scrapy.exporters.BaseItemExporter.finish_exporting" title="scrapy.exporters.BaseItemExporter.finish_exporting"><code class="xref py py-meth docutils literal notranslate"><span class="pre">finish_exporting()</span></code></a> 发出输出过程结束的信号</p>
<p>在这里你可以看到 <a class="reference internal" href="item-pipeline.html"><span class="doc">Item Pipeline</span></a> 它使用多个项目导出器，根据其中一个字段的值将 爬取  的项目分组到不同的文件中：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">itemadapter</span> <span class="kn">import</span> <span class="n">ItemAdapter</span>
<span class="kn">from</span> <span class="nn">scrapy.exporters</span> <span class="kn">import</span> <span class="n">XmlItemExporter</span>

<span class="k">class</span> <span class="nc">PerYearXmlExportPipeline</span><span class="p">:</span>
    <span class="sd">&quot;&quot;&quot;Distribute items across multiple XML files according to their &#39;year&#39; field&quot;&quot;&quot;</span>

    <span class="k">def</span> <span class="nf">open_spider</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">spider</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">year_to_exporter</span> <span class="o">=</span> <span class="p">{}</span>

    <span class="k">def</span> <span class="nf">close_spider</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">spider</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">exporter</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">year_to_exporter</span><span class="o">.</span><span class="n">values</span><span class="p">():</span>
            <span class="n">exporter</span><span class="o">.</span><span class="n">finish_exporting</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">_exporter_for_item</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">):</span>
        <span class="n">adapter</span> <span class="o">=</span> <span class="n">ItemAdapter</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
        <span class="n">year</span> <span class="o">=</span> <span class="n">adapter</span><span class="p">[</span><span class="s1">&#39;year&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="n">year</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">year_to_exporter</span><span class="p">:</span>
            <span class="n">f</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="sa">f</span><span class="s1">&#39;</span><span class="si">{</span><span class="n">year</span><span class="si">}</span><span class="s1">.xml&#39;</span><span class="p">,</span> <span class="s1">&#39;wb&#39;</span><span class="p">)</span>
            <span class="n">exporter</span> <span class="o">=</span> <span class="n">XmlItemExporter</span><span class="p">(</span><span class="n">f</span><span class="p">)</span>
            <span class="n">exporter</span><span class="o">.</span><span class="n">start_exporting</span><span class="p">()</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">year_to_exporter</span><span class="p">[</span><span class="n">year</span><span class="p">]</span> <span class="o">=</span> <span class="n">exporter</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">year_to_exporter</span><span class="p">[</span><span class="n">year</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">process_item</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">item</span><span class="p">,</span> <span class="n">spider</span><span class="p">):</span>
        <span class="n">exporter</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_exporter_for_item</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
        <span class="n">exporter</span><span class="o">.</span><span class="n">export_item</span><span class="p">(</span><span class="n">item</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">item</span>
</pre></div>
</div>
</div>
<div class="section" id="serialization-of-item-fields">
<span id="topics-exporters-field-serialization"></span><h2>项字段的序列化<a class="headerlink" href="#serialization-of-item-fields" title="永久链接至标题">¶</a></h2>
<p>默认情况下，字段值是未修改地传递给基础序列化库的，如何序列化字段值的决定被委托给每个特定的序列化库。</p>
<p>但是，您可以自定义每个字段值的序列化方式。 <em>在传递到序列化库之前</em> .</p>
<p>有两种方法可以自定义字段序列化的方式，下面将介绍这两种方法。</p>
<div class="section" id="declaring-a-serializer-in-the-field">
<span id="topics-exporters-serializers"></span><h3>1。在字段中声明序列化程序<a class="headerlink" href="#declaring-a-serializer-in-the-field" title="永久链接至标题">¶</a></h3>
<p>如果你使用 <a class="reference internal" href="items.html#scrapy.item.Item" title="scrapy.item.Item"><code class="xref py py-class docutils literal notranslate"><span class="pre">Item</span></code></a> 可以在中声明序列化程序 <a class="reference internal" href="items.html#topics-items-fields"><span class="std std-ref">field metadata</span></a> . 序列化程序必须是可调用的，它接收值并返回其序列化形式。</p>
<p>例子：：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">scrapy</span>

<span class="k">def</span> <span class="nf">serialize_price</span><span class="p">(</span><span class="n">value</span><span class="p">):</span>
    <span class="k">return</span> <span class="sa">f</span><span class="s1">&#39;$ </span><span class="si">{</span><span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="si">}</span><span class="s1">&#39;</span>

<span class="k">class</span> <span class="nc">Product</span><span class="p">(</span><span class="n">scrapy</span><span class="o">.</span><span class="n">Item</span><span class="p">):</span>
    <span class="n">name</span> <span class="o">=</span> <span class="n">scrapy</span><span class="o">.</span><span class="n">Field</span><span class="p">()</span>
    <span class="n">price</span> <span class="o">=</span> <span class="n">scrapy</span><span class="o">.</span><span class="n">Field</span><span class="p">(</span><span class="n">serializer</span><span class="o">=</span><span class="n">serialize_price</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="overriding-the-serialize-field-method">
<h3>2。重写serialize_field（）方法<a class="headerlink" href="#overriding-the-serialize-field-method" title="永久链接至标题">¶</a></h3>
<p>您还可以覆盖 <a class="reference internal" href="#scrapy.exporters.BaseItemExporter.serialize_field" title="scrapy.exporters.BaseItemExporter.serialize_field"><code class="xref py py-meth docutils literal notranslate"><span class="pre">serialize_field()</span></code></a> 方法自定义字段值的导出方式。</p>
<p>确保调用基类 <a class="reference internal" href="#scrapy.exporters.BaseItemExporter.serialize_field" title="scrapy.exporters.BaseItemExporter.serialize_field"><code class="xref py py-meth docutils literal notranslate"><span class="pre">serialize_field()</span></code></a> 方法。</p>
<p>例子：：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">scrapy.exporter</span> <span class="kn">import</span> <span class="n">XmlItemExporter</span>

<span class="k">class</span> <span class="nc">ProductXmlExporter</span><span class="p">(</span><span class="n">XmlItemExporter</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">serialize_field</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">field</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">field</span> <span class="o">==</span> <span class="s1">&#39;price&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="sa">f</span><span class="s1">&#39;$ </span><span class="si">{</span><span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="si">}</span><span class="s1">&#39;</span>
        <span class="k">return</span> <span class="nb">super</span><span class="p">(</span><span class="n">Product</span><span class="p">,</span> <span class="bp">self</span><span class="p">)</span><span class="o">.</span><span class="n">serialize_field</span><span class="p">(</span><span class="n">field</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="built-in-item-exporters-reference">
<span id="topics-exporters-reference"></span><h2>内置项导出器引用<a class="headerlink" href="#built-in-item-exporters-reference" title="永久链接至标题">¶</a></h2>
<p>这是与 Scrapy  捆绑在一起的物品出口商名单。其中一些包含输出示例，假设您正在导出这两个项：</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Item</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;Color TV&#39;</span><span class="p">,</span> <span class="n">price</span><span class="o">=</span><span class="s1">&#39;1200&#39;</span><span class="p">)</span>
<span class="n">Item</span><span class="p">(</span><span class="n">name</span><span class="o">=</span><span class="s1">&#39;DVD player&#39;</span><span class="p">,</span> <span class="n">price</span><span class="o">=</span><span class="s1">&#39;200&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="section" id="baseitemexporter">
<h3>BaseItemExporter<a class="headerlink" href="#baseitemexporter" title="永久链接至标题">¶</a></h3>
<dl class="py class">
<dt id="scrapy.exporters.BaseItemExporter">
<em class="property">class </em><code class="sig-prename descclassname">scrapy.exporters.</code><code class="sig-name descname">BaseItemExporter</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">fields_to_export</span><span class="o">=</span><span class="default_value">None</span></em>, <em class="sig-param"><span class="n">export_empty_fields</span><span class="o">=</span><span class="default_value">False</span></em>, <em class="sig-param"><span class="n">encoding</span><span class="o">=</span><span class="default_value">'utf-8'</span></em>, <em class="sig-param"><span class="n">indent</span><span class="o">=</span><span class="default_value">0</span></em>, <em class="sig-param"><span class="n">dont_fail</span><span class="o">=</span><span class="default_value">False</span></em><span class="sig-paren">)</span><a class="headerlink" href="#scrapy.exporters.BaseItemExporter" title="永久链接至目标">¶</a></dt>
<dd><p>这是所有项目导出器的（抽象）基类。它支持所有（具体）项目导出器使用的公共特性，例如定义要导出的字段、是否导出空字段或要使用的编码。</p>
<p>这些功能可以通过 <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 填充各自实例属性的方法参数： <a class="reference internal" href="#scrapy.exporters.BaseItemExporter.fields_to_export" title="scrapy.exporters.BaseItemExporter.fields_to_export"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fields_to_export</span></code></a> ， <a class="reference internal" href="#scrapy.exporters.BaseItemExporter.export_empty_fields" title="scrapy.exporters.BaseItemExporter.export_empty_fields"><code class="xref py py-attr docutils literal notranslate"><span class="pre">export_empty_fields</span></code></a> ， <a class="reference internal" href="#scrapy.exporters.BaseItemExporter.encoding" title="scrapy.exporters.BaseItemExporter.encoding"><code class="xref py py-attr docutils literal notranslate"><span class="pre">encoding</span></code></a> ， <a class="reference internal" href="#scrapy.exporters.BaseItemExporter.indent" title="scrapy.exporters.BaseItemExporter.indent"><code class="xref py py-attr docutils literal notranslate"><span class="pre">indent</span></code></a> .</p>
<div class="versionadded">
<p><span class="versionmodified added">2.0 新版功能: </span>这个 <em>dont_fail</em> 参数。</p>
</div>
<dl class="py method">
<dt id="scrapy.exporters.BaseItemExporter.export_item">
<code class="sig-name descname">export_item</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">item</span></em><span class="sig-paren">)</span><a class="headerlink" href="#scrapy.exporters.BaseItemExporter.export_item" title="永久链接至目标">¶</a></dt>
<dd><p>导出给定项。此方法必须在子类中实现。</p>
</dd></dl>

<dl class="py method">
<dt id="scrapy.exporters.BaseItemExporter.serialize_field">
<code class="sig-name descname">serialize_field</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">field</span></em>, <em class="sig-param"><span class="n">name</span></em>, <em class="sig-param"><span class="n">value</span></em><span class="sig-paren">)</span><a class="headerlink" href="#scrapy.exporters.BaseItemExporter.serialize_field" title="永久链接至目标">¶</a></dt>
<dd><p>返回给定字段的序列化值。如果要控制特定字段或值的序列化/导出方式，可以重写此方法（在自定义项导出器中）。</p>
<p>默认情况下，此方法查找序列化程序 <a class="reference internal" href="#topics-exporters-serializers"><span class="std std-ref">declared in the item field</span></a> 并返回将该序列化程序应用于值的结果。如果找不到序列化程序，则返回未更改的值。</p>
<dl class="field-list simple">
<dt class="field-odd">参数</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>field</strong> (<a class="reference internal" href="items.html#scrapy.item.Field" title="scrapy.item.Field"><code class="xref py py-class docutils literal notranslate"><span class="pre">Field</span></code></a> object or a <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> instance) -- 正在序列化的字段。如果来源 <a class="reference internal" href="items.html#item-types"><span class="std std-ref">item object</span></a> 不定义字段元数据， <em>领域</em> 是空的 <a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#dict" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">dict</span></code></a> .</p></li>
<li><p><strong>name</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(在 Python v3.9)"><em>str</em></a>) -- 正在序列化的字段的名称</p></li>
<li><p><strong>value</strong> -- 正在序列化的值</p></li>
</ul>
</dd>
</dl>
</dd></dl>

<dl class="py method">
<dt id="scrapy.exporters.BaseItemExporter.start_exporting">
<code class="sig-name descname">start_exporting</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#scrapy.exporters.BaseItemExporter.start_exporting" title="永久链接至目标">¶</a></dt>
<dd><p>指示导出过程的开始。某些导出器可能会使用此命令生成某些必需的头（例如， <a class="reference internal" href="#scrapy.exporters.XmlItemExporter" title="scrapy.exporters.XmlItemExporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">XmlItemExporter</span></code></a> ）在导出任何项之前必须调用此方法。</p>
</dd></dl>

<dl class="py method">
<dt id="scrapy.exporters.BaseItemExporter.finish_exporting">
<code class="sig-name descname">finish_exporting</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#scrapy.exporters.BaseItemExporter.finish_exporting" title="永久链接至目标">¶</a></dt>
<dd><p>发出输出过程结束的信号。一些导出器可能会使用此命令生成一些必需的页脚（例如， <a class="reference internal" href="#scrapy.exporters.XmlItemExporter" title="scrapy.exporters.XmlItemExporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">XmlItemExporter</span></code></a> ）在没有其他要导出的项之后，必须始终调用此方法。</p>
</dd></dl>

<dl class="py attribute">
<dt id="scrapy.exporters.BaseItemExporter.fields_to_export">
<code class="sig-name descname">fields_to_export</code><a class="headerlink" href="#scrapy.exporters.BaseItemExporter.fields_to_export" title="永久链接至目标">¶</a></dt>
<dd><p>包含要导出的字段名称的列表，或 <code class="docutils literal notranslate"><span class="pre">None</span></code> 如果要导出所有字段。默认为 <code class="docutils literal notranslate"><span class="pre">None</span></code> .</p>
<p>一些出口商（如 <a class="reference internal" href="#scrapy.exporters.CsvItemExporter" title="scrapy.exporters.CsvItemExporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">CsvItemExporter</span></code></a> ）遵守此属性中定义的字段的顺序。</p>
<p>使用时 <a class="reference internal" href="items.html#item-types"><span class="std std-ref">item objects</span></a> 如果不公开所有可能的字段，则不支持导出每个项目的不同字段子集的导出器将只导出在导出的第一个项目中找到的字段。使用 <code class="docutils literal notranslate"><span class="pre">fields_to_export</span></code> 定义要导出的所有字段。</p>
</dd></dl>

<dl class="py attribute">
<dt id="scrapy.exporters.BaseItemExporter.export_empty_fields">
<code class="sig-name descname">export_empty_fields</code><a class="headerlink" href="#scrapy.exporters.BaseItemExporter.export_empty_fields" title="永久链接至目标">¶</a></dt>
<dd><p>是否在导出的数据中包含空/未填充的项字段。默认为 <code class="docutils literal notranslate"><span class="pre">False</span></code> . 一些出口商（如 <a class="reference internal" href="#scrapy.exporters.CsvItemExporter" title="scrapy.exporters.CsvItemExporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">CsvItemExporter</span></code></a> ）忽略此属性并始终导出所有空字段。</p>
<p>对dict项忽略此选项。</p>
</dd></dl>

<dl class="py attribute">
<dt id="scrapy.exporters.BaseItemExporter.encoding">
<code class="sig-name descname">encoding</code><a class="headerlink" href="#scrapy.exporters.BaseItemExporter.encoding" title="永久链接至目标">¶</a></dt>
<dd><p>输出字符编码。</p>
</dd></dl>

<dl class="py attribute">
<dt id="scrapy.exporters.BaseItemExporter.indent">
<code class="sig-name descname">indent</code><a class="headerlink" href="#scrapy.exporters.BaseItemExporter.indent" title="永久链接至目标">¶</a></dt>
<dd><p>用于在每个级别上缩进输出的空间量。默认为 <code class="docutils literal notranslate"><span class="pre">0</span></code> .</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">indent=None</span></code> 选择最紧凑的表示形式，同一行中的所有项都没有缩进</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">indent&lt;=0</span></code> 每个项目都在自己的行上，没有缩进</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">indent&gt;0</span></code> 每一项都在自己的行上，用提供的数值缩进</p></li>
</ul>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="pythonitemexporter">
<h3>PythonItemExporter<a class="headerlink" href="#pythonitemexporter" title="永久链接至标题">¶</a></h3>
</div>
<div class="section" id="xmlitemexporter">
<h3>XmlItemExporter<a class="headerlink" href="#xmlitemexporter" title="永久链接至标题">¶</a></h3>
<dl class="py class">
<dt id="scrapy.exporters.XmlItemExporter">
<em class="property">class </em><code class="sig-prename descclassname">scrapy.exporters.</code><code class="sig-name descname">XmlItemExporter</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">file</span></em>, <em class="sig-param"><span class="n">item_element</span><span class="o">=</span><span class="default_value">'item'</span></em>, <em class="sig-param"><span class="n">root_element</span><span class="o">=</span><span class="default_value">'items'</span></em>, <em class="sig-param"><span class="o">**</span><span class="n">kwargs</span></em><span class="sig-paren">)</span><a class="headerlink" href="#scrapy.exporters.XmlItemExporter" title="永久链接至目标">¶</a></dt>
<dd><p>将XML格式的项导出到指定的文件对象。</p>
<dl class="field-list simple">
<dt class="field-odd">参数</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>file</strong> -- 用于导出数据的类似文件的对象。它的 <code class="docutils literal notranslate"><span class="pre">write</span></code> 方法应接受 <code class="docutils literal notranslate"><span class="pre">bytes</span></code> （以二进制模式打开的磁盘文件， <code class="docutils literal notranslate"><span class="pre">io.BytesIO</span></code> 物体等）</p></li>
<li><p><strong>root_element</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(在 Python v3.9)"><em>str</em></a>) -- 导出的XML中根元素的名称。</p></li>
<li><p><strong>item_element</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(在 Python v3.9)"><em>str</em></a>) -- 导出的XML中每个item元素的名称。</p></li>
</ul>
</dd>
</dl>
<p>此的其他关键字参数 <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 方法传递给 <a class="reference internal" href="#scrapy.exporters.BaseItemExporter" title="scrapy.exporters.BaseItemExporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseItemExporter</span></code></a>  <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 方法。</p>
<p>该出口商的典型产出为：</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;items&gt;
  &lt;item&gt;
    &lt;name&gt;Color TV&lt;/name&gt;
    &lt;price&gt;1200&lt;/price&gt;
 &lt;/item&gt;
  &lt;item&gt;
    &lt;name&gt;DVD player&lt;/name&gt;
    &lt;price&gt;200&lt;/price&gt;
 &lt;/item&gt;
&lt;/items&gt;
</pre></div>
</div>
<p>除非在 <code class="xref py py-meth docutils literal notranslate"><span class="pre">serialize_field()</span></code> 方法，多值字段通过序列化 <code class="docutils literal notranslate"><span class="pre">&lt;value&gt;</span></code> 元素。这是为了方便起见，因为多值字段非常常见。</p>
<p>例如，项目：</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>Item(name=[&#39;John&#39;, &#39;Doe&#39;], age=&#39;23&#39;)
</pre></div>
</div>
<p>将序列化为：</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;items&gt;
  &lt;item&gt;
    &lt;name&gt;
      &lt;value&gt;John&lt;/value&gt;
      &lt;value&gt;Doe&lt;/value&gt;
    &lt;/name&gt;
    &lt;age&gt;23&lt;/age&gt;
  &lt;/item&gt;
&lt;/items&gt;
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="csvitemexporter">
<h3>CsvItemExporter<a class="headerlink" href="#csvitemexporter" title="永久链接至标题">¶</a></h3>
<dl class="py class">
<dt id="scrapy.exporters.CsvItemExporter">
<em class="property">class </em><code class="sig-prename descclassname">scrapy.exporters.</code><code class="sig-name descname">CsvItemExporter</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">file</span></em>, <em class="sig-param"><span class="n">include_headers_line</span><span class="o">=</span><span class="default_value">True</span></em>, <em class="sig-param"><span class="n">join_multivalued</span><span class="o">=</span><span class="default_value">','</span></em>, <em class="sig-param"><span class="n">errors</span><span class="o">=</span><span class="default_value">None</span></em>, <em class="sig-param"><span class="o">**</span><span class="n">kwargs</span></em><span class="sig-paren">)</span><a class="headerlink" href="#scrapy.exporters.CsvItemExporter" title="永久链接至目标">¶</a></dt>
<dd><p>将CSV格式的项目导出到给定的类似文件的对象。如果 <code class="xref py py-attr docutils literal notranslate"><span class="pre">fields_to_export</span></code> 属性已设置，将用于定义csv列及其顺序。这个 <code class="xref py py-attr docutils literal notranslate"><span class="pre">export_empty_fields</span></code> 属性对此导出程序没有影响。</p>
<dl class="field-list simple">
<dt class="field-odd">参数</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>file</strong> -- 用于导出数据的类似文件的对象。它的 <code class="docutils literal notranslate"><span class="pre">write</span></code> 方法应接受 <code class="docutils literal notranslate"><span class="pre">bytes</span></code> （以二进制模式打开的磁盘文件， <code class="docutils literal notranslate"><span class="pre">io.BytesIO</span></code> 物体等）</p></li>
<li><p><strong>include_headers_line</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(在 Python v3.9)"><em>str</em></a>) -- 如果启用，则使导出器输出一个标题行，其中字段名取自 <a class="reference internal" href="#scrapy.exporters.BaseItemExporter.fields_to_export" title="scrapy.exporters.BaseItemExporter.fields_to_export"><code class="xref py py-attr docutils literal notranslate"><span class="pre">BaseItemExporter.fields_to_export</span></code></a> 或第一个导出项字段。</p></li>
<li><p><strong>join_multivalued</strong> -- 如果找到，将用于联接多值字段的字符。</p></li>
<li><p><strong>errors</strong> (<a class="reference external" href="https://docs.python.org/3/library/stdtypes.html#str" title="(在 Python v3.9)"><em>str</em></a>) -- 指定如何处理编码和解码错误的可选字符串。有关详细信息，请参阅 <a class="reference external" href="https://docs.python.org/3/library/io.html#io.TextIOWrapper" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.TextIOWrapper</span></code></a> .</p></li>
</ul>
</dd>
</dl>
<p>此的其他关键字参数 <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 方法传递给 <a class="reference internal" href="#scrapy.exporters.BaseItemExporter" title="scrapy.exporters.BaseItemExporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseItemExporter</span></code></a>  <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 方法，以及对 <a class="reference external" href="https://docs.python.org/3/library/csv.html#csv.writer" title="(在 Python v3.9)"><code class="xref py py-func docutils literal notranslate"><span class="pre">csv.writer()</span></code></a> 函数，因此您可以使用任何 <a class="reference external" href="https://docs.python.org/3/library/csv.html#csv.writer" title="(在 Python v3.9)"><code class="xref py py-func docutils literal notranslate"><span class="pre">csv.writer()</span></code></a> 自定义此导出器的函数参数。</p>
<p>该出口商的典型产出为：</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>product,price
Color TV,1200
DVD player,200
</pre></div>
</div>
</dd></dl>

</div>
<div class="section" id="pickleitemexporter">
<h3>PickleItemExporter<a class="headerlink" href="#pickleitemexporter" title="永久链接至标题">¶</a></h3>
<dl class="py class">
<dt id="scrapy.exporters.PickleItemExporter">
<em class="property">class </em><code class="sig-prename descclassname">scrapy.exporters.</code><code class="sig-name descname">PickleItemExporter</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">file</span></em>, <em class="sig-param"><span class="n">protocol</span><span class="o">=</span><span class="default_value">0</span></em>, <em class="sig-param"><span class="o">**</span><span class="n">kwargs</span></em><span class="sig-paren">)</span><a class="headerlink" href="#scrapy.exporters.PickleItemExporter" title="永久链接至目标">¶</a></dt>
<dd><p>将pickle格式的项导出到给定的类文件对象。</p>
<dl class="field-list simple">
<dt class="field-odd">参数</dt>
<dd class="field-odd"><ul class="simple">
<li><p><strong>file</strong> -- 用于导出数据的类似文件的对象。它的 <code class="docutils literal notranslate"><span class="pre">write</span></code> 方法应接受 <code class="docutils literal notranslate"><span class="pre">bytes</span></code> （以二进制模式打开的磁盘文件， <code class="docutils literal notranslate"><span class="pre">io.BytesIO</span></code> 物体等）</p></li>
<li><p><strong>protocol</strong> (<a class="reference external" href="https://docs.python.org/3/library/functions.html#int" title="(在 Python v3.9)"><em>int</em></a>) -- 要使用的pickle协议。</p></li>
</ul>
</dd>
</dl>
<p>有关详细信息，请参阅 <a class="reference external" href="https://docs.python.org/3/library/pickle.html#module-pickle" title="(在 Python v3.9)"><code class="xref py py-mod docutils literal notranslate"><span class="pre">pickle</span></code></a> .</p>
<p>此的其他关键字参数 <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 方法传递给 <a class="reference internal" href="#scrapy.exporters.BaseItemExporter" title="scrapy.exporters.BaseItemExporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseItemExporter</span></code></a>  <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 方法。</p>
<p>pickle不是人类可读的格式，因此没有提供输出示例。</p>
</dd></dl>

</div>
<div class="section" id="pprintitemexporter">
<h3>PprintItemExporter<a class="headerlink" href="#pprintitemexporter" title="永久链接至标题">¶</a></h3>
<dl class="py class">
<dt id="scrapy.exporters.PprintItemExporter">
<em class="property">class </em><code class="sig-prename descclassname">scrapy.exporters.</code><code class="sig-name descname">PprintItemExporter</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">file</span></em>, <em class="sig-param"><span class="o">**</span><span class="n">kwargs</span></em><span class="sig-paren">)</span><a class="headerlink" href="#scrapy.exporters.PprintItemExporter" title="永久链接至目标">¶</a></dt>
<dd><p>以漂亮的打印格式将项导出到指定的文件对象。</p>
<dl class="field-list simple">
<dt class="field-odd">参数</dt>
<dd class="field-odd"><p><strong>file</strong> -- 用于导出数据的类似文件的对象。它的 <code class="docutils literal notranslate"><span class="pre">write</span></code> 方法应接受 <code class="docutils literal notranslate"><span class="pre">bytes</span></code> （以二进制模式打开的磁盘文件， <code class="docutils literal notranslate"><span class="pre">io.BytesIO</span></code> 物体等）</p>
</dd>
</dl>
<p>此的其他关键字参数 <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 方法传递给 <a class="reference internal" href="#scrapy.exporters.BaseItemExporter" title="scrapy.exporters.BaseItemExporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseItemExporter</span></code></a>  <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 方法。</p>
<p>该出口商的典型产出为：</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>{&#39;name&#39;: &#39;Color TV&#39;, &#39;price&#39;: &#39;1200&#39;}
{&#39;name&#39;: &#39;DVD player&#39;, &#39;price&#39;: &#39;200&#39;}
</pre></div>
</div>
<p>较长的行（如果存在）的格式很好。</p>
</dd></dl>

</div>
<div class="section" id="jsonitemexporter">
<h3>JsonItemExporter<a class="headerlink" href="#jsonitemexporter" title="永久链接至标题">¶</a></h3>
<dl class="py class">
<dt id="scrapy.exporters.JsonItemExporter">
<em class="property">class </em><code class="sig-prename descclassname">scrapy.exporters.</code><code class="sig-name descname">JsonItemExporter</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">file</span></em>, <em class="sig-param"><span class="o">**</span><span class="n">kwargs</span></em><span class="sig-paren">)</span><a class="headerlink" href="#scrapy.exporters.JsonItemExporter" title="永久链接至目标">¶</a></dt>
<dd><p>将JSON格式的项导出到指定的类文件对象，将所有对象作为对象列表写入。额外的 <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 方法参数传递给 <a class="reference internal" href="#scrapy.exporters.BaseItemExporter" title="scrapy.exporters.BaseItemExporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseItemExporter</span></code></a>  <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 方法，以及对 <a class="reference external" href="https://docs.python.org/3/library/json.html#json.JSONEncoder" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">JSONEncoder</span></code></a>  <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 方法，因此可以使用任何 <a class="reference external" href="https://docs.python.org/3/library/json.html#json.JSONEncoder" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">JSONEncoder</span></code></a>  <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 方法参数自定义此导出器。</p>
<dl class="field-list simple">
<dt class="field-odd">参数</dt>
<dd class="field-odd"><p><strong>file</strong> -- 用于导出数据的类似文件的对象。它的 <code class="docutils literal notranslate"><span class="pre">write</span></code> 方法应接受 <code class="docutils literal notranslate"><span class="pre">bytes</span></code> （以二进制模式打开的磁盘文件， <code class="docutils literal notranslate"><span class="pre">io.BytesIO</span></code> 物体等）</p>
</dd>
</dl>
<p>该出口商的典型产出为：</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>[{&quot;name&quot;: &quot;Color TV&quot;, &quot;price&quot;: &quot;1200&quot;},
{&quot;name&quot;: &quot;DVD player&quot;, &quot;price&quot;: &quot;200&quot;}]
</pre></div>
</div>
<div class="admonition warning" id="json-with-large-data">
<p class="admonition-title">警告</p>
<p>JSON是一种非常简单和灵活的序列化格式，但是由于是增量的（aka），它不能很好地扩展到大量的数据中。流模式）解析在JSON解析器（在任何语言上）中都不受很好的支持（如果有），而且大多数解析器只是解析内存中的整个对象。如果您希望JSON的强大性和简单性具有更流友好的格式，请考虑使用 <a class="reference internal" href="#scrapy.exporters.JsonLinesItemExporter" title="scrapy.exporters.JsonLinesItemExporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">JsonLinesItemExporter</span></code></a> 或者将输出分成多个块。</p>
</div>
</dd></dl>

</div>
<div class="section" id="jsonlinesitemexporter">
<h3>JsonLinesItemExporter<a class="headerlink" href="#jsonlinesitemexporter" title="永久链接至标题">¶</a></h3>
<dl class="py class">
<dt id="scrapy.exporters.JsonLinesItemExporter">
<em class="property">class </em><code class="sig-prename descclassname">scrapy.exporters.</code><code class="sig-name descname">JsonLinesItemExporter</code><span class="sig-paren">(</span><em class="sig-param"><span class="n">file</span></em>, <em class="sig-param"><span class="o">**</span><span class="n">kwargs</span></em><span class="sig-paren">)</span><a class="headerlink" href="#scrapy.exporters.JsonLinesItemExporter" title="永久链接至目标">¶</a></dt>
<dd><p>将JSON格式的项导出到指定的类似文件的对象，每行写入一个JSON编码的项。额外的 <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 方法参数传递给 <a class="reference internal" href="#scrapy.exporters.BaseItemExporter" title="scrapy.exporters.BaseItemExporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseItemExporter</span></code></a>  <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 方法，以及对 <a class="reference external" href="https://docs.python.org/3/library/json.html#json.JSONEncoder" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">JSONEncoder</span></code></a>  <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 方法，因此可以使用任何 <a class="reference external" href="https://docs.python.org/3/library/json.html#json.JSONEncoder" title="(在 Python v3.9)"><code class="xref py py-class docutils literal notranslate"><span class="pre">JSONEncoder</span></code></a>  <code class="docutils literal notranslate"><span class="pre">__init__</span></code> 方法参数自定义此导出器。</p>
<dl class="field-list simple">
<dt class="field-odd">参数</dt>
<dd class="field-odd"><p><strong>file</strong> -- 用于导出数据的类似文件的对象。它的 <code class="docutils literal notranslate"><span class="pre">write</span></code> 方法应接受 <code class="docutils literal notranslate"><span class="pre">bytes</span></code> （以二进制模式打开的磁盘文件， <code class="docutils literal notranslate"><span class="pre">io.BytesIO</span></code> 物体等）</p>
</dd>
</dl>
<p>该出口商的典型产出为：</p>
<div class="highlight-none notranslate"><div class="highlight"><pre><span></span>{&quot;name&quot;: &quot;Color TV&quot;, &quot;price&quot;: &quot;1200&quot;}
{&quot;name&quot;: &quot;DVD player&quot;, &quot;price&quot;: &quot;200&quot;}
</pre></div>
</div>
<p>不同于 <a class="reference internal" href="#scrapy.exporters.JsonItemExporter" title="scrapy.exporters.JsonItemExporter"><code class="xref py py-class docutils literal notranslate"><span class="pre">JsonItemExporter</span></code></a> ，此导出程序生成的格式非常适合序列化大量数据。</p>
</dd></dl>

</div>
<div class="section" id="marshalitemexporter">
<h3>MarshalItemExporter<a class="headerlink" href="#marshalitemexporter" title="永久链接至标题">¶</a></h3>
</div>
</div>
</div>


           </div>
           
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="../news.html" class="btn btn-neutral float-right" title="发行说明" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="signals.html" class="btn btn-neutral float-left" title="信号" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        
        &copy; 版权所有 2008–2020, Scrapy developers
      <span class="lastupdated">
        最后更新于 10月 18, 2020.
      </span>

    </p>
  </div>
    
    
    
    Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a
    
    <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a>
    
    provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  

  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
  
 
<script type="text/javascript">
!function(){var analytics=window.analytics=window.analytics||[];if(!analytics.initialize)if(analytics.invoked)window.console&&console.error&&console.error("Segment snippet included twice.");else{analytics.invoked=!0;analytics.methods=["trackSubmit","trackClick","trackLink","trackForm","pageview","identify","reset","group","track","ready","alias","page","once","off","on"];analytics.factory=function(t){return function(){var e=Array.prototype.slice.call(arguments);e.unshift(t);analytics.push(e);return analytics}};for(var t=0;t<analytics.methods.length;t++){var e=analytics.methods[t];analytics[e]=analytics.factory(e)}analytics.load=function(t){var e=document.createElement("script");e.type="text/javascript";e.async=!0;e.src=("https:"===document.location.protocol?"https://":"http://")+"cdn.segment.com/analytics.js/v1/"+t+"/analytics.min.js";var n=document.getElementsByTagName("script")[0];n.parentNode.insertBefore(e,n)};analytics.SNIPPET_VERSION="3.1.0";
analytics.load("8UDQfnf3cyFSTsM4YANnW5sXmgZVILbA");
analytics.page();
}}();

analytics.ready(function () {
    ga('require', 'linker');
    ga('linker:autoLink', ['scrapinghub.com', 'crawlera.com']);
});
</script>


</body>
</html>